package org.dawfinn.droiditarian.server.webview;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dawfinn.droiditarian.DeedLocationQuery;
import org.dawfinn.droiditarian.client.webview.WebView;
import org.dawfinn.droiditarian.server.data.DeedLocation;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class DeedLocationPhotoServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String websafeKey = req.getParameter(WebView.KEY_PARAM);
		Key deedKey = KeyFactory.stringToKey(websafeKey);

		DeedLocation deed = DeedLocationQuery.findByKey(deedKey);

		Blob image = deed.getImage();

		resp.setContentType("image/jpeg");
		ServletOutputStream output = resp.getOutputStream();
		if (image != null)
			output.write(image.getBytes());
			
		output.close();
	}
}
